/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kindatamanip;

import java.util.ArrayList;

/**
 *
 * @author thbrandston
 */
public class Measurements {
    
    public static int avgSharedAlleles(ArrayList<DipOrg> sibGroup)
    {
        int weightSum = 0;
        int n = 0;
        for(int j = 0; j < sibGroup.size()-1; j++)
        {
            for(int k = j+1; k < sibGroup.size(); k++)
            {
                n++;
                weightSum += compareAlleles(sibGroup.get(k), sibGroup.get(j));
            }
        }

        if(n > 0)
            weightSum /= n;
        else
            weightSum = -1;

        return weightSum;
    }
    
    public static int avgSharedAlleles(ArrayList<DipOrg> sibGroup1, ArrayList<DipOrg> sibGroup2)
    {
        int weightSum = 0;
        int n = 0;
        for(int k = 0; k < sibGroup1.size(); k++)
        {
            for(int l = 0; l < sibGroup2.size(); l++)
            {
                n++;
                weightSum += Measurements.compareAlleles(sibGroup1.get(k), sibGroup2.get(l));
            }
        }

        if(n > 0)
            weightSum /= n;
        else
            weightSum = -1;

        return weightSum;
    }

    public static int compareAlleles(DipOrg org1, DipOrg org2)
    {
        if(org1.loci.length != org2.loci.length)
            return -1;

        int n = 0;

        for(int i = 0; i < org1.loci.length; i++)
        {
            if(org1.loci[i][0] == org2.loci[i][0] ||
               org1.loci[i][0] == org2.loci[i][1])
                n++;
            if(org1.loci[i][1] == org2.loci[i][0] ||
               org1.loci[i][1] == org2.loci[i][1])
                n++;
        }

        return n;
    }
}
